package com.macro.mall.portal.service.impl;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.macro.mall.mapper.BmsInterfaceConfigMapper;
import com.macro.mall.model.BmsInterfaceConfig;
import com.macro.mall.portal.dto.BmsInterfaceConfigParam;
import com.macro.mall.portal.service.BmsInterfaceConfigService;
import com.macro.mall.portal.util.UserUtils;

import cn.hutool.core.date.DateTime;

@Service
public class BmsInterfaceConfigServiceImpl implements BmsInterfaceConfigService {
    
    @Autowired
    private BmsInterfaceConfigMapper bmsInterfaceConfigMapper;
    
    @Autowired
    private UserUtils userUtils;

    @Override
    public int create(BmsInterfaceConfigParam bmsInterfaceConfigParam) {
        BmsInterfaceConfig bmsInterfaceConfig = new BmsInterfaceConfig(); 
        BeanUtils.copyProperties(bmsInterfaceConfigParam, bmsInterfaceConfig);
        Long userId = userUtils.getUserId();
        bmsInterfaceConfig.setCreateUser(userId);
        bmsInterfaceConfig.setUpdateUser(userId);
        bmsInterfaceConfig.setCreateTime(new DateTime());
        bmsInterfaceConfig.setUpdateTime(new DateTime());
        int count = bmsInterfaceConfigMapper.insertBmsInterfaceConfig(bmsInterfaceConfig);
        return count;
    }

    @Override
    public int update(BmsInterfaceConfigParam bmsInterfaceConfigParam) {
        BmsInterfaceConfig bmsInterfaceConfig = new BmsInterfaceConfig(); 
        BeanUtils.copyProperties(bmsInterfaceConfigParam, bmsInterfaceConfig);
        Long userId = userUtils.getUserId();
        bmsInterfaceConfig.setUpdateUser(userId);
        bmsInterfaceConfig.setUpdateTime(new DateTime());
        int count = bmsInterfaceConfigMapper.updateBmsInterfaceConfig(bmsInterfaceConfig);
        return count;
    }

    @Override
    public List<BmsInterfaceConfig> getList(BmsInterfaceConfigParam bmsInterfaceConfigParam) {
        PageHelper.startPage(bmsInterfaceConfigParam.getPageNum(), bmsInterfaceConfigParam.getPageSize());
        BmsInterfaceConfig bmsInterfaceConfig = new BmsInterfaceConfig();
        bmsInterfaceConfig.setName(bmsInterfaceConfigParam.getName());
        bmsInterfaceConfig.setCode(bmsInterfaceConfigParam.getCode());
        return bmsInterfaceConfigMapper.list(bmsInterfaceConfig);
    }

    @Override
    public BmsInterfaceConfig getItem(Long id) {
        return bmsInterfaceConfigMapper.selectById(id);
    }

    @Override
    public int delete(List<Long> ids) {
        //获取当前登录用户id
        Long userId = userUtils.getUserId();
        
        int count = bmsInterfaceConfigMapper.updateDeleteId(ids,userId);
        return count;
    }

	@Override
	public BmsInterfaceConfig getItemByCode(String code) {
		return bmsInterfaceConfigMapper.selectByCode(code);
	}
}
